from rest_framework import serializers

from app.health_manage.models import ServerHealth


class CreateUpdateSerializer(serializers.ModelSerializer):
    createAt = serializers.DateTimeField(read_only=True, format="%Y-%m-%d %H:%M:%S")
    updateAt = serializers.DateTimeField(read_only=True, format="%Y-%m-%d %H:%M:%S")


class ServerHealthSerializer(CreateUpdateSerializer):
    """资产类型字典"""

    warrantySince = serializers.DateField(read_only=True, format="%Y-%m-%d")
    warrantyEnd = serializers.DateField(read_only=True, format="%Y-%m-%d")
    arrivalDate = serializers.DateField(read_only=True, format="%Y-%m-%d")
    calculateTime = serializers.DateTimeField(read_only=True, format="%Y-%m-%d %H:%M:%S")
    frameInfo = serializers.SerializerMethodField()
    position = serializers.SerializerMethodField()

    class Meta:
        model = ServerHealth
        fields = '__all__'

    def get_frameInfo(self, obj):
        if obj.uLocationStart and obj.uLocationEnd:
            return obj.uLocationStart + '-' + obj.uLocationEnd
        else:
            return ''

    def get_position(self, obj):
        if obj.dataCenter and obj.rackCode:
            return obj.dataCenter + '/' + obj.rackBuilding + '/' + str(obj.rackFloor) + '层/' + obj.rackModule + '/' + obj.rackCode
